<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * View Topic
 * File: viewtopic.php
 * Purpose: Showing all topics within a forum
 * Todo: Finish writing it
 *
 */
				
require('system/files/global.php');

// start the initalisation of the main page (forum page)
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
	// is using an id
	$sql = "SELECT `topic_id`, `topic_title`, `topic_prefix`, `topic_lupdate`, UNIX_TIMESTAMP(topic_lupdate) AS `unix_time`, `topic_starter`, `forum_name`, `forum_type`, `forum_parent`, `forum_id` FROM `nbb_topics` LEFT JOIN `nbb_forums` ON `nbb_topics`.`topic_forum_id` = `nbb_forums`.`forum_id` WHERE `topic_id` = '" . Db_Abstraction::clean($_GET['id']) . "' LIMIT 1";
	$topic_info = Db_Abstraction::fetch_array($sql);
	
	if(count($topic_info) > 1) {
		// is a real result
		// start the initalisation of the header/topic page
		$core_template_system->setvar(array('page_title' => $topic_info['topic_title'] . ' Topic',
											'load_reflection_js' => true));
		
		// show the header loaded
		$core_template_system->pparse();
		
		// include the language file for this page
		$core_lang_system->reset();
		$core_lang_system->add_file('viewtopic');
		
		// was this a new post and does it need to be set as "read"?
		if(CoreUserSystem::isLoggedIn()) {
			if(isset($_SESSION['User']['tracking_table'][$topic_info['forum_id']]['marked_read']) && $_SESSION['User']['tracking_table'][$topic_info['forum_id']]['marked_read'] > $topic_info['unix_time']) {
				// no new posts
			} else {
				// marked read hasn't been set
				// is the last login timestamp more than last post
				if($_SESSION['User']['user_la1'] > $topic_info['unix_time']) {
					// no new posts
				} else {
					// have we already "read" this topic?
					if(isset($_SESSION['User']['tracking_table'][$topic_info['forum_id']][$row['topic_id']]) && $_SESSION['User']['tracking_table'][$topic_info['forum_id']][$topic_info['topic_id']] > $topic_info['unix_time']) {
						// no new posts
					} else {
						// are new posts, set as read
						$_SESSION['User']['tracking_table'][$topic_info['forum_id']][$topic_info['topic_id']] = time();
					}
				}
			}
		}
		
		// startup the pagination system
		include(BASE_PATH . '/system/classes/lite/langcore_lite_class.php');
		include(BASE_PATH . '/system/classes/pagination_class.php');
		$pagination_system = new pagination;
		
		// lets initialise the pagination system and see what page we need
		$calc_posts_sql = "SELECT count(post_id) FROM `nbb_posts` WHERE `topic_id` = '" . $topic_info['topic_id'] . "'";
		$calc_posts_res = Db_Abstraction::fetch_array($calc_posts_sql);
		$pagination_system->setvar(Db_Abstraction::clean($_GET['page']), $calc_posts_res[0], $topic_info['topic_id'], 3);
		$core_template_system->setvar('show_pagination', $pagination_system->displayPagination());
		
		// continue with the rest of the loading
		$posts_sql = "SELECT `post_id`, `post_title`, `post_user_id`, `post_time`, `text`, `user_name`, `user_id`, `user_signature`, `session_info` FROM `nbb_posts` INNER JOIN `nbb_posts_text` ON `nbb_posts`.`post_id` = `nbb_posts_text`.`post_text_id` LEFT JOIN `nbb_users` ON `nbb_posts`.`post_user_id` = `nbb_users`.`user_id` LEFT JOIN `nbb_sessions` ON `nbb_users`.`user_name` = `nbb_sessions`.`session_info` WHERE `topic_id` = '" . $topic_info['topic_id'] . "' LIMIT " . $pagination_system->getLimit();
		$posts_res = Db_Abstraction::query($posts_sql);
		
		// build this into a loop and pass it into the template
		$i = 0; $loop = array();
		while($obj = DB_Abstraction::fetch_assoc($posts_res)) {
			// is this user online or offline?
			if($obj['session_info'] == (NULL || '')) {
				// is offline
				$online = false;
			} else {
				// is online
				$online = true;
			}
			
			// form an array
			$loop[$i] = array('post_id' 	  => $obj['post_id'],
							  'post_title'	  => $obj['post_title'],
							  'post_user_id'  => $obj['post_user_id'],
							  'post_time' 	  => $obj['post_time'],
							  'post_text' 	  => $obj['text'],
							  'user_name' 	  => $obj['user_name'],
							  'has_avatar'    => file_exists('storedfiles/avatars/' . $result['user_name'] . '.png'),
							  'user_id' 	  => $obj['user_id'],
							  'user_signature'=> $obj['user_signature'],
							  'profile_link'  => urlSystem::viewUrl(2, array('user_id' => $obj['user_id'],
																			 'user_name' => $obj['user_name'])),
							  'is_online'	  => $online);
			
			// increment array
			$i++;
		}
		
		// build the menu
		$breadcrumb_nav = array('settings' => array('level_down' => false,
													'main_class' => 'menu'),
								'information' => array('home' => array('data' => array(1 => 'Home',
																					   2 => 1))));
		
		// is this a subforum?
		if($topic_info['forum_type'] == 2) {
			// is a subforum
			// what's the parent forum?
			$parent_sql = "SELECT `forum_name`, `forum_id` FROM `nbb_forums` WHERE `forum_id` = '" . $topic_info['forum_parent'] . "'";
			$parent_result = Db_Abstraction::fetch_array($parent_sql);
			
			$breadcrumb_nav['information']['parent_forum'] = array('data' => array(1 => $parent_result['forum_name'],
																				   2 => 1,
																				   3 => array('forum_id' => $parent_result['forum_id'])));
		}
		
		// build the current forum
		$breadcrumb_nav['information']['forum'] = array('data' => array(1 => $topic_info['forum_name'],
																		2 => 1,
																		3 => array('forum_id' => $topic_info['forum_id'])));
		
		// build the current topic
		$breadcrumb_nav['information']['topic'] = array('data' => array(1 => $topic_info['topic_title'],
																		2 => 3,
																		3 => array('topic_id' => $topic_info['topic_id'])));
		
		// add some variables
		$core_template_system->setloop('posts', $loop);
		$core_template_system->setvar(array('topic_name' => $topic_info['topic_title'],
											'breadcrumb' => $core_nav_system->generateNav($breadcrumb_nav)));
		
		// render the page
		$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/viewtopic.html');
		$core_template_system->pparse();
	} else {
		// is fake and doesn't exist
		// start the initalisation of the header
		$core_template_system->setvar('page_title', 'Error');
		
		// show the header loaded
		$core_template_system->pparse();
		
		// include the language file for this page
		$core_lang_system->reset();
		$core_lang_system->add_file('viewtopic');

		// produce the error, render the error page (this forum id doesn't actually exist)
		$core_template_system->setvar('error_message', $core_lang_system->get_var('TOPIC_DOESNT_EXIST', 3));
		$core_template_system->setvar('error', $core_lang_system->get_var('ERROR', 4));
		$core_template_system->setvar('back_url', 'index.php');

		// build the menu
		$core_template_system->setvar('breadcrumb', $core_nav_system->generateNav(array('settings' => array('level_down' => false,
																											'main_class' => 'menu'),
																						'information' => array('home' => array('data' => array(1 => 'Home',
																																	 		   2 => 1))))));

		$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
		$core_template_system->pparse();
	}
}

require('system/files/footer.php');
?>